/* vcc/plugins/emerg/emerg.c 
 * 
 * This file is part of vcc. 
 * 
 * vcc is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version. 
 * 
 * vcc is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License 
 * along with vcc. If not, see <https://www.gnu.org/licenses/>
 */ 




#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

#include <vcc/vcc.h>
#include <vcc/pretty.h>
#include <vcc/hooks.h>
#include <vcc/plugin.h>
#include <vcc/interfaces.h>
#include <vcc/version.h>


int do_cmd_emerg(int argc, char **argv);

struct cmdtable emerg_cmdtab[] = {
	{ "-emerg", do_cmd_emerg, "Enter emergency mode. " }, 

	{ NULL, NULL, NULL }
};

static char *emergtab[] = {
	"NEARLY DIED. PREPARE FOR THE ARMAGEDDON. ", 
	"EMERGENCY. NO MESSAGES INCLUDING ENGLISH. ", 
	"DANGEROUS. LATIUM OR OTHER LANGUAGES. ", 
	"WARNING. ENGLISH IS OK. ", 
	"EARLY WARNING. CHINESE IS OK. ", 
	"NOTHING. LEFT TO EAT OR DRINK. ", 
	NULL, 
};
#define EMERGLEVELS 5

struct plugin plg_emerg = {
	.state = 0, 
	.name = "emerg", 
	.descr = "Emergency mode control. ", 
	.cmdtab = emerg_cmdtab, 
};

#define this_plg (&plg_emerg)

static int enter_emergency_mode(int level) {
	char msg[MSG_SIZE];

	sprintf(msg, "-cqd#%s is on emergency mode %d. %s\n", usrname, level, emergtab[level]);
	send_msg(msg, usrname);

	system("clear");
	system("${SHELL:-/bin/bash}");
	send_msg("-cqd#emergency mode out. \n", usrname);

	return 0;
}

int do_cmd_emerg(int argc, char **argv) {
	int level;

	plg_check_enabled();

	switch (argc) {
	case 1:
		/* the max level */
		level = 0;
		break;

	case 2:
		level = atoi(argv[1]);
		break;

	default:
		/* enter the max level because people often do wrong things
		 * when they're in a hurry */
		level = 0;
		break;
	}

	if (level > EMERGLEVELS) 
		/* who care these things when they're going to die? */
		level = 0;
	enter_emergency_mode(level);
	return 0;
}


